﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using DBA.Data.ADO;

namespace DBA.Web.ProjectManage
{
    public partial class DownFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = Request.Params["type"];
            string id = Request.Params["id"];
            DataTable dt = getFileName(id);
            string filePath = string.Empty;
            switch (type) 
            {
                case "1"://表示科研
                    filePath = Server.MapPath("/upload/project/");
                    break;
                case "2"://表示科研
                    filePath = Server.MapPath("/upload/paper/");
                    break;
                case "3"://表示著作
                    filePath = Server.MapPath("/upload/book/");
                    break;
                case "4"://表示获奖
                    filePath = Server.MapPath("/upload/awards/");
                    break;
            }


            string fileCode = dt.Rows[0]["PaCode"].ToString();//客户端保存的文件名 
            string fielName = dt.Rows[0]["PaName"].ToString();
            filePath += fileCode;

            //以字符流的形式下载文件 
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开 
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fielName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();

        }

        protected DataTable getFileName(string id) 
        {
            string strSql = "select * from PorjectAttachment where Paid=@Paid ";
            DataAccessLibrary da = new DataAccessLibrary();

            da.ManagerParameter.AddNewInParameter("Paid", id);

            DataTable dt = da.ExecuteFillDataTable(strSql);
            return dt;
        }
    }
}